home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Code Resources / Meter Control / Test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-01  |  6.7 KB  |  327 lines  |  [TEXT/KAHL]

  1. /**************
  2. ** Test.c
  3. **
  4. ** Shows how to use the Meter controls.
  5. ***************/
  6.  
  7. #include <MacHeaders>
  8. #include "Meter.h"
  9.  
  10. static void Init(void);
  11. static void SetupMenus(void);
  12. static void Cleanup(void);
  13. static void DoCommand(long mResult);
  14. static void DoKeyDown(EventRecord *theEvent);
  15. static void DoMouseDown(EventRecord *theEvent);
  16. static void DoControl( EventRecord *theEvent, WindowPtr);
  17. static void DoAbout(void);
  18. static void DoUpdate(EventRecord *theEvent);
  19.  
  20. MenuHandle gAppleM, gFileM;
  21. int gSleep = 15;
  22. ControlHandle    gDial1, gDial2, gDial3, gDial4;
  23. WindowPtr        gMainWindow;
  24.  
  25.  
  26. #define kMenuBar        128
  27. #define kAppleM            128
  28. #define kAppleAboutItem    1
  29. #define kFileM            129
  30. #define kFileQuitItem    1
  31. #define kMainWind        128
  32. #define kAboutDLOG        128
  33. #define kAboutOK        1        /* OK button */
  34.  
  35. #define kDial1CNTL        128
  36. #define kDial2CNTL        129
  37. #define kDial3CNTL        130
  38. #define kDial4CNTL        131
  39.  
  40.  
  41. static void Init(void)
  42. {
  43.     register int i;
  44.     InitGraf(&qd.thePort);
  45.     InitFonts();
  46.     InitWindows();
  47.     InitMenus();
  48.     TEInit();
  49.     InitDialogs( (ProcPtr)NULL);
  50.     InitCursor();
  51.     for (i=0; i<7; i++) (void)MoreMasters();    
  52.     FlushEvents(everyEvent, 0);
  53.     SetEventMask(everyEvent);
  54. } // Init()
  55.  
  56.  
  57.  
  58. static void SetupMenus(void)
  59. {
  60.     Handle mbar = GetNewMBar( kMenuBar);
  61.     if (mbar != NULL)
  62.         SetMenuBar( mbar);
  63.     else
  64.     {
  65.         SysBeep(1); SysBeep(1);
  66.         ExitToShell();
  67.     }
  68.     gAppleM = GetMenu( kAppleM);
  69.     if (gAppleM == NULL)
  70.     {
  71.         SysBeep(1); SysBeep(1);
  72.         ExitToShell();
  73.     }
  74.     AddResMenu( gAppleM, 'DRVR');
  75.     DrawMenuBar();
  76. } // SetupMenus()
  77.  
  78.  
  79. main()
  80. {
  81.     EventRecord theEvent;
  82.     
  83.     Init();
  84.     SetupMenus();
  85.     
  86.     gMainWindow = GetNewCWindow( kMainWind, NULL, (WindowPtr) -1L);
  87.     if (gMainWindow == NULL)
  88.     {
  89.         SysBeep(5); SysBeep(5); SysBeep(5);
  90.         ExitToShell();
  91.     } // if
  92.  
  93.     gDial1 = GetNewControl( kDial1CNTL, gMainWindow);
  94.     gDial2 = GetNewControl( kDial2CNTL, gMainWindow);
  95.     gDial3 = GetNewControl( kDial3CNTL, gMainWindow);
  96.     gDial4 = GetNewControl( kDial4CNTL, gMainWindow);
  97.     SetPort( gMainWindow);
  98.     ShowWindow(gMainWindow);
  99.     SelectWindow(gMainWindow);
  100.     DrawControls(gMainWindow);
  101.     
  102.     while (1)                        // wait for mouse button
  103.         if (WaitNextEvent(everyEvent, &theEvent, gSleep, NULL))
  104.         {
  105.             switch (theEvent.what) {
  106.                 case mouseDown:
  107.                     DoMouseDown( &theEvent);
  108.                     break;
  109.                 case keyDown:
  110.                 case autoKey:
  111.                     DoKeyDown( &theEvent);
  112.                     break;
  113.                 case updateEvt:
  114.                     DoUpdate( &theEvent);
  115.                     break;
  116.             } // switch 
  117.         } // if
  118.  
  119. } // main()
  120.  
  121.  
  122.  
  123. static void Cleanup(void)
  124. {
  125.     KillControls( gMainWindow);
  126.     DisposeWindow( gMainWindow);
  127.     ExitToShell();
  128. } // Cleanup()
  129.  
  130.  
  131. static void DoCommand(long mResult)
  132. {
  133.     int theItem = LoWord(mResult);
  134.     int theMenu = HiWord(mResult);
  135.     Str255 name;
  136.     int temp;
  137.     
  138.     switch (theMenu) {
  139.         case kAppleM:
  140.             if (theItem == kAppleAboutItem) {
  141.                 DoAbout();
  142.             } else {
  143.                 GetItem( gAppleM, theItem, name);
  144.                 temp = OpenDeskAcc(name);
  145.             }
  146.             break;
  147.             
  148.         case kFileM:
  149.             switch (theItem) {
  150.                 case kFileQuitItem:
  151.                     Cleanup();
  152.                     break;
  153.             } /* switch theItem */
  154.             break;
  155.             
  156.     } /* switch theMenu */
  157.     HiliteMenu(0);        /* unhilite the selected menu */
  158. } // DoCommand()
  159.  
  160.  
  161. static void DoKeyDown(EventRecord *theEvent)
  162. {
  163.     char theChar;
  164.     static int state=0;
  165.     static int values[] = { 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160,
  166.                             176, 192, 208, 224, 240, 256};
  167. #define maxStates 17
  168.     
  169.     theChar = (char)theEvent->message & charCodeMask;
  170.     if (theEvent->modifiers & cmdKey)
  171.         DoCommand( MenuKey( theChar));
  172.     else    
  173.     {
  174.         if (theChar == '+')
  175.         {    if (++state == maxStates) state = 0; }
  176.         else
  177.         if (theChar == '-')
  178.             if (--state == -1) state = maxStates-1;
  179.  
  180.         SetCtlValue( gDial1, values[state]);
  181.         SetCtlValue( gDial2, values[state]);
  182.         SetCtlValue( gDial3, values[state]);
  183.         SetCtlValue( gDial4, values[state]);
  184.     }
  185. } // DoKeyDown()
  186.  
  187.  
  188. static void DoMouseDown(EventRecord *theEvent)
  189. {
  190.     WindowPtr whichWindow;
  191.     Rect dragRect;
  192.     int  part;
  193.     
  194.     dragRect = qd.screenBits.bounds;
  195.     InsetRect( &dragRect, -4, -4);
  196.     
  197.     part = FindWindow(theEvent->where, &whichWindow);
  198.     
  199.     switch(part) {
  200.         case inMenuBar:
  201.             DoCommand( MenuSelect(theEvent->where) );
  202.             break;
  203.         case inContent:
  204.             DoControl( theEvent, whichWindow);
  205.             break;
  206.         case inDrag:
  207.             DragWindow( whichWindow, theEvent->where, &dragRect);
  208.             break;
  209.         case inGrow:
  210.             break;
  211.         case inGoAway:
  212.             if (TrackGoAway( whichWindow, theEvent->where) )
  213.                 Cleanup();
  214.             break;
  215.     } // switch
  216. } // DoMouseDown()
  217.  
  218.  
  219. static void DoControl( EventRecord *theEvent, WindowPtr whichWindow)
  220. {
  221.     ControlHandle theControl;
  222.     int part, newpart;
  223.     int value;
  224.     
  225.     GlobalToLocal( &theEvent->where);
  226.     part = FindControl( theEvent->where, whichWindow, &theControl);
  227.     if ( !part)
  228.         return;
  229.     
  230. /*
  231. **  This code here (checking for the command key) doesn't work because
  232. **  of some quirk I've not figured out with the Control Manager.  The
  233. **  control works perfectly as planned/noted otherwise, but you just
  234. **  can't drag it around.  Bummer, but IM is very vague about some things.
  235. **
  236.     if ( theEvent->modifiers & cmdKey)
  237.     {
  238.         DragControl( theControl, theEvent->where, &whichWindow->portRect,
  239.                     &whichWindow->portRect, noConstraint);
  240.         DrawControls( whichWindow);
  241.     }
  242. **
  243. */
  244.  
  245.     switch( part) {
  246.     
  247.     case inNeedle:        /* same as inThumb for scroll bars */
  248.         TrackControl( theControl, theEvent->where, NULL);
  249.         break;
  250.         
  251.     case inTurnDown:        /* same as inPageUp for scroll bars */
  252.         HiliteControl( theControl, inTurnDown);
  253.         while (StillDown())
  254.         {
  255.             Point p;
  256.             GetMouse( &p);
  257.             if ( part == (newpart=TestControl( theControl, p)) )
  258.             {
  259.                 value = GetCtlValue( theControl);
  260.                 SetCtlValue(theControl, value - 4);
  261.             }
  262.         }
  263.         HiliteControl( theControl, 0);
  264.         break;
  265.         
  266.     case inTurnUp:        /* same as inPageDown for scroll bars */
  267.         HiliteControl( theControl, inTurnUp);
  268.         while (StillDown())
  269.         {
  270.             Point p;
  271.             GetMouse( &p);
  272.             if ( part == (newpart=TestControl( theControl, p)) )
  273.             {
  274.                 value = GetCtlValue( theControl);
  275.                 SetCtlValue(theControl, value + 4);
  276.             }
  277.         }
  278.         HiliteControl( theControl, 0);
  279.         break;
  280.         
  281.     case inUpButton:                /* not implemented for meters */
  282.         HiliteControl( theControl, inUpButton);
  283.         while (StillDown())
  284.         {
  285.             value = GetCtlValue( theControl);
  286.             SetCtlValue(theControl, value - 2);
  287.         }
  288.         HiliteControl( theControl, 0);
  289.         break;
  290.         
  291.     case inDownButton:                /* not implemented for meters */
  292.         HiliteControl( theControl, inDownButton);
  293.         while (StillDown())
  294.         {
  295.             value = GetCtlValue( theControl);
  296.             SetCtlValue(theControl, value + 2);
  297.         }
  298.         HiliteControl( theControl, 0);
  299.         break;
  300.     }
  301. } // DoControl()
  302.  
  303.  
  304.  
  305. static void DoAbout(void)
  306. {
  307.     DialogPtr    about;
  308.     int         item = 99;
  309.     
  310.     about = GetNewDialog( kAboutDLOG, NULL, (WindowPtr)-1L);
  311.     if (about == NULL)
  312.     { SysBeep(1); return; }
  313.     
  314.     ShowWindow( about);
  315.     while ( item != kAboutOK)
  316.         ModalDialog( NULL, &item);
  317.     DisposDialog( about);
  318. } /* DoAbout() */
  319.  
  320.  
  321. static void DoUpdate(EventRecord *theEvent)
  322. {
  323.     WindowPtr whichWindow = (WindowPtr)theEvent->message;
  324.     BeginUpdate( whichWindow);
  325.     DrawControls( whichWindow);
  326.     EndUpdate( whichWindow);
  327. }